package com.chenyuhan.myview.testview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import com.chenyuhan.myview.R;

/**
 * Created by Administrator on 2017/6/28 0028.
 */

public class TestVolumeView extends View {
    private final String TAG = TestVolumeView.class.getSimpleName();
    /**
     * 第一层圆圈的颜色
     */
    private int mFirstColor;
    /**
     * 第二层圆圈的颜色
     */
    private int mSecondColor;
    /**
     * 圆圈的宽度
     */
    private int mCircleWidth;
    /**
     * 中心图片大小
     */
    private Bitmap mImage;
    /**
     * 音量大小
     */
    private int mVolume;
    /**
     * 将音量分为几块
     */
    private int mVolumeNumber;
    /**
     * 画笔
     */
    private Paint mPaint;

    /**
     * 是否画下一个圆环
     */
    private boolean isNext;
    private Rect mBound;

    public TestVolumeView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TestVolumeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initSet();
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TestVolumeView, defStyleAttr, 0);
        for (int i = 0; i < typedArray.getIndexCount(); i++) {
            int attr = typedArray.getIndex(i);
            switch (attr) {
                case R.styleable.TestVolumeView_firstColor:
                    //默认颜色设置为黑色
                    mFirstColor = typedArray.getInt(attr, Color.BLACK);
                    break;
                case R.styleable.TestVolumeView_secondColor:
                    // 默认颜色设置为白色
                    mSecondColor = typedArray.getColor(attr, Color.WHITE);
                    break;
                case R.styleable.TestVolumeView_circleWidth:
                    //设置圆圈的宽度
                    mCircleWidth = typedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.TestVolumeView_image:
                    mImage = BitmapFactory.decodeResource(getResources(), typedArray.getResourceId(attr, 0));
                    break;
                case R.styleable.TestVolumeView_volumeNumber:
                    mVolumeNumber = typedArray.getInt(attr, 10);
                    break;
            }
        }
        typedArray.recycle();
    }

    /**
     * 初始化默认值
     */
    private void initSet() {
        mFirstColor = Color.BLACK;
        mSecondColor = Color.WHITE;
        mCircleWidth = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics());
        mImage = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        mVolumeNumber = 0;
        mVolume = 0;
    }
    @Override
    protected void onDraw(Canvas canvas) {
        mPaint = new Paint();
        drawImg(canvas);
        try {
            drawOval(canvas);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void drawImg(Canvas canvas) {
        //设置图片的矩形大小
        Rect imgRect = new Rect();
        imgRect.left = 0;
        imgRect.right = mImage.getWidth();
        imgRect.top = 0;
        imgRect.bottom = mImage.getHeight();
        //计算居中的矩形范围
        imgRect.left = getWidth() / 2 - imgRect.right / 2;
        imgRect.right = getWidth() / 2 + imgRect.right / 2;
        imgRect.top = getHeight() / 2 - imgRect.bottom / 2;
        imgRect.bottom = getHeight() / 2 + imgRect.bottom / 2;

        canvas.drawBitmap(mImage, null, imgRect, mPaint);
    }

    private void drawOval(Canvas canvas) throws Exception {
        mPaint.setStrokeWidth(mCircleWidth);// 设置圆环的宽度
        mPaint.setAntiAlias(true); // 消除锯齿
        mPaint.setStyle(Paint.Style.STROKE); // 设置空心
        mPaint.setStrokeCap(Paint.Cap.ROUND); // 定义线段断电形状为圆头
        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;
        int r = centerX - mCircleWidth / 2;//中心圆半径

        RectF rectF = new RectF(centerX - r, centerY - r, centerX + r, centerY + r);
        mPaint.setColor(mFirstColor);//设置第一层圆环的颜色
        //根据音量块数量设置间隔值大小
        float intervalSize = 0;
        if (mVolumeNumber < 0) return;
        if (mVolumeNumber < 5)
            intervalSize = 40;
        else if (mVolumeNumber < 10)
            intervalSize = 20;
        else if (mVolumeNumber < 15)
            intervalSize = 15;
        else if (mVolumeNumber < 20)
            intervalSize = 12;
        else if (mVolumeNumber < 30)
            intervalSize = 10;
        else if (mVolumeNumber < 50)
            intervalSize = 6;
        else if (mVolumeNumber < 80)
            intervalSize = 4;
        else if (mVolumeNumber < 160)
            intervalSize = 2;
        else if (mVolumeNumber < 360)
            intervalSize = 1;
        else if (mVolumeNumber > 360)
            throw new Exception("不能超过360音量块");

        //计算每一块音量显示虽占的比例,
        float volumSize = (360 * 1.0f - intervalSize * mVolumeNumber) / mVolumeNumber;
        for (int i = 0; i < mVolumeNumber; i++) {
            //参数（从左到右）：圆弧所在的椭圆对象;圆弧的起始角度;圆弧的角度;是否显示半径连线，true表示显示圆弧与圆心的半径连线，false表示不显示;绘制时所使用的画笔
            canvas.drawArc(rectF, i * (volumSize + intervalSize), volumSize, false, mPaint);
        }
        mPaint.setColor(mSecondColor);//设置第二层圆环的颜色
        for (int i = 0; i < mVolume; i++) {
            canvas.drawArc(rectF, i * (volumSize + intervalSize), volumSize, false, mPaint);
        }
    }

    /**
     * 音量上升
     */
    public void up() {
        if (mVolume < mVolumeNumber) {
            mVolume++;
        }
        postInvalidate();
    }

    /**
     * 音量下降
     */
    public void down() {
        if (mVolume > 0) {
            mVolume--;
        }
        postInvalidate();
    }

    private int xDown, xMove;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xDown = (int) event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                xMove = (int) event.getY();
                //设置5是为了防止加减过快
                if (xMove > xDown + 10) {
                    down();
                    xDown = xMove;
                } else if (xMove < xDown - 10) {
                    up();
                    xDown = xMove;
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }

        return true;
    }
}
